package net.yambal.mobile.adv.managed_component
{
	import flash.desktop.NativeApplication;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	
	import mx.core.mx_internal;
	import mx.events.FlexEvent;
	
	import net.yambal.mobile.adv.ADV_Chapter;
	import net.yambal.mobile.adv.managed_component.base.IiADV_View;
	import net.yambal.mobile.adv.manager.ADV_AppMng;
	
	import spark.components.ViewNavigatorApplication;
	import spark.transitions.CrossFadeViewTransition;
	
	public class ADV_ViewNavigatorApplication extends ViewNavigatorApplication
	{
		private var _app_mng:ADV_AppMng = null;
		
		public var storyEmbedXMLCls:Class;
		public var homeView:Class = null;
		public var storyView:Class = null;
		public var resouce_cls:Class = null;
		public var flashShowMS:Number = 300;// ms
		
		
		//private var _currentViewType:String;
		
		public function ADV_ViewNavigatorApplication()
		{
			super();

			//_currentViewType = "flash";
				
			creationPolicy = "all";
			_app_mng = ADV_AppMng.getInstance();
			addEventListener(FlexEvent.CREATION_COMPLETE, ADV_onCreationComplete, false, 0, true);
		}
		
		private function ADV_onCreationComplete(e:FlexEvent):void{
			new resouce_cls();
			removeEventListener(FlexEvent.CREATION_COMPLETE, ADV_onCreationComplete);
			_app_mng.onViewNaviAppCreationComplete(this);
			
			// Transition
			navigator.defaultPushTransition = new CrossFadeViewTransition();

			// KeyboardEvent
			NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			
			
			NativeApplication.nativeApplication.addEventListener(Event.DEACTIVATE, onDeactivate);
		}
		
		
		private function onKeyDown(e:KeyboardEvent):void{
			e.preventDefault();
			trace("KeyboardEvent");
			
			var actV:IiADV_View = navigator.activeView as IiADV_View;
			
			if(actV != null){
				switch (e.keyCode)  {
					case Keyboard.BACK:
						actV.onClickBackBtn();
						break;
					case Keyboard.SEARCH:
						actV.onClickSearchBtn();
						break;
					case Keyboard.MENU:
						actV.onClickMenuBtn();
						break;
				}
			}

		}
		private function onKeyUp(e:KeyboardEvent):void{
			trace("onKeyUp");
			e.preventDefault();
		}
		
		private function onDeactivate(e:Event):void{
			trace(this + ":onDeactivate");
			_app_mng.exit();
		}
		
		// Home(View)に移動
		public function goHome():void{
			if(homeView != null){
				navigator.replaceView(homeView);//.pushView(homeView);
			}
		}
		
		// Story(View)に移動
		public function showStory():void{
			var actV:IiADV_View = navigator.activeView as IiADV_View;
			
			if(storyView != null && actV.type != "story"){
				navigator.popAll();
				navigator.pushView(storyView);
			}
		}
		
		/**
		 * チャプタ移動の通知を受ける
		 * （移動自体はStoryViewが行う）
		 **/
		public function pushChapterParagraph(chapter_id:String, paragraph_id:String = null, option:Object = null):void{
			trace(this + ":pushChapterParagraph" +
				"\n\t受信");
			
			showStory();
		}
	}
}